//
//  AudioPlayer.m
//  AudioDemo
//
//  Created by Hailong.wang on 15/9/25.
//  Copyright (c) 2015年 Hailong.wang. All rights reserved.
//

#import "AudioPlayer.h"

static AudioPlayer *_player = nil;

@implementation AudioPlayer

+ (AudioPlayer *)shareSingleton {
    @synchronized (self) {
        if (!_player) {
            _player = [[self alloc] init];
        }
    }
    return _player;
}

- (id)init {
    self = [super init];
    if (self) {
    }
    return self;
}

- (void)startPlayWithURL:(NSString *)url {
    //如果在播放就停止
    if ([_player isPlaying]) {
        [_player stop];
        _player = nil;
    }
    //初始化播放器，将文件地址传入
    /*
     AVAudioPlayer 无法边下边播，如果加载网络音频，需要全部下载到本地，然后进行播放
     */
    NSError *error;
    _player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:url] error:&error];
    
    if (error) {
        NSLog(@"%@",error);
    }
    
    if (_player) {
        NSLog(@"player begin to play");
        [_player play];

    }
    //播放
    //[_player play];
}

- (void)pausePlay {
    if ([_player isPlaying]) {
        [_player pause];
    } else {
        [_player play];
    }
}

- (void)stopPlay {
    if (_player) {
        [_player stop];
        _player = nil;
    }
}

@end





